<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Article;
use JPush\Client as JPush;

class PushMsg extends AdminBase
{
    public function index()
    {
        return $this->fetch('index');
    }

    public function pushall($message,$extra = array('push_class'=>'all'))
    {
        $client = new JPush("5b7f8f7c748bbbf5c1a8eb74", "bbc498e4bda963d2c3fb4047");

        $pusher = $client->push();
        $pusher->setPlatform('all');
        $pusher->addAllAudience();
        $pusher->iosNotification($message,array(
            'sound' => 'sound.caf',
            'extras' => $extra,
        ));

        $pusher->send();
        $this->success("推送成功");
    }

    public function pushArticle($id)
    {
        $article_info = Article::where('id',$id)->find();

        $message = $article_info['author']."发布了:".$article_info['title']."。";
        $extra = array(
            'article_id' => $id,
			'push_class' => 'article',
			'title' => $article_info['title'],
			'content' => $article_info['introduction'],
        );

        $this->pushall($message,$extra);

        $this->success("推送成功");
    }
}